﻿using ASmile.Extend;
using ASmile.Win.Controls;
using System;
using System.Windows.Forms;

namespace ASmile.Win.Forms
{
    public partial class FrmSelectArea : FrmBaseModal
    {
        public FrmSelectArea()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 用于返回的地址详情
        /// </summary>
        public RetArea AreaResult { get; } = new RetArea();

        string SearchPCode { set; get; } = string.Empty;

        object GetArea(string search)
        {
            var r = DictHelper.GetAreas(search, SearchPCode);
            return r;
        }

        private void FrmSelectArea_Load(object sender, EventArgs e)
        {
            InitCoBoxGrid(
                new ArgInitCoBoxGrid(txtArea1, GetArea),
                new ArgInitCoBoxGrid(txtArea2, GetArea),
                new ArgInitCoBoxGrid(txtArea3, GetArea));

            CoBoxGrid.OnBeforeShowGridView += CoBoxGrid_OnBeforeShow;
            CoBoxGrid.OnSelectedRowItem += CoBoxGrid_OnSelected;
            dcm1.SetObjectToControl(AreaResult);
        }

        private void CoBoxGrid_OnSelected(EditText txt, object selModel)
        {
            //var m = selModel as Dict_Area;
            if (txtArea1 == txt)
            {
                txtArea2.Focus();
            }
            else if (txtArea2 == txt)
            {
                txtArea3.Focus();
            }
            else if (txtArea3 == txt)
            {
                txtDetails.Focus();
            }
        }

        private void CoBoxGrid_OnBeforeShow(ComboxGridArgs obj)
        {
            if (obj.TxtBox == txtArea2)
            {
                SearchPCode = txtArea1.ValueItem?.ToString();
                if (SearchPCode.IsEmpty())
                {
                    obj.CancelShow = true;
                    txtArea1.SelectAll();
                    txtArea1.Focus();
                    ShowMessage("请先选择上级区域");
                }
            }
            else if (obj.TxtBox == txtArea3)
            {
                SearchPCode = txtArea2.ValueItem?.ToString();
                if (SearchPCode.IsEmpty())
                {
                    obj.CancelShow = true;
                    txtArea2.SelectAll();
                    txtArea2.Focus();
                    ShowMessage("请先选择上级区域");
                }
            }
            else
            {
                SearchPCode = string.Empty;
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            dcm1.GetObjectByControl(AreaResult);
            this.DialogResult = DialogResult.OK;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtArea1_TextChanged(object sender, EventArgs e)
        {
            if (txtArea1.ValueItem == null)
            {
                txtArea2.SetDefault();
                txtArea2.SetDefault();
            }
        }

        private void txtArea2_TextChanged(object sender, EventArgs e)
        {
            if (txtArea2.ValueItem == null)
            {
                txtArea3.SetDefault();
            }
        }

        private void txtDetails_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnOK.Focus();
            }
        }
    }
    /// <summary>
    /// 区域返回结果
    /// </summary>
    public class RetArea
    {
        /// <summary>
        /// 分隔符，ToString时使用
        /// </summary>
        public string Split { set; get; } = string.Empty;
        /// <summary>
        /// 省、直辖市 代码
        /// </summary>
        public string Area1Code { set; get; }
        /// <summary>
        /// 省、直辖市 名称
        /// </summary>
        public string Area1Name { set; get; }
        /// <summary>
        /// 市 代码
        /// </summary>
        public string Area2Code { set; get; }
        /// <summary>
        /// 市 名称
        /// </summary>
        public string Area2Name { set; get; }
        /// <summary>
        /// 县、区代码
        /// </summary>
        public string Area3Code { set; get; }
        /// <summary>
        /// 县、区名称
        /// </summary>
        public string Area3Name { set; get; }
        /// <summary>
        /// 其它详情录入的地址
        /// </summary>
        public string Details { set; get; }
        /// <summary>
        /// 返回地址的所有文本
        /// </summary>
        public override string ToString()
        {
            string r = Area1Name;
            if (Area2Name.IsNotEmpty()) r += Split;
            r += Area2Name;
            if (Area3Name.IsNotEmpty()) r += Split;
            r += Area3Name;
            if (Details.IsNotEmpty()) r += Split;
            r += Details.Trim();
            return r;
        }
    }
}
